package com.ruoyi.health.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.health.domain.NoticeCategory;
import com.ruoyi.health.mapper.NoticeCategoryMapper;
import com.ruoyi.health.mapper.NoticeDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.health.domain.NoticeDetail;
import com.ruoyi.health.mapper.NoticeMapper;
import com.ruoyi.health.domain.Notice;
import com.ruoyi.health.service.INoticeService;

/**
 * 养老资讯Service业务层处理
 * 
 * @author dalian
 * @date 2023-08-05
 */
@Service
public class NoticeServiceImpl implements INoticeService 
{
    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeDetailMapper noticeDetailMapper;

    @Autowired
    private NoticeCategoryMapper noticeCategoryMapper;

    /**
     * 查询养老资讯(带详情 )
     * 
     * @param id 养老资讯主键
     * @return 养老资讯
     */
    @Override
    public Notice selectNoticeById(Long id)
    {
        Notice notice = noticeMapper.selectNoticeById(id);
        //二次查询
        NoticeDetail noticeDetail = noticeDetailMapper.selectNoticeDetailById(id);
        notice.setNoticeDetail(noticeDetail);
        return notice;
    }

    /**
     * 查询养老资讯列表
     * 
     * @param notice 养老资讯
     * @return 养老资讯
     */
    @Override
    public List<Notice> selectNoticeList(Notice notice,Long cate1Id) {
        //分情况，按照一级、二级分类查询
        if(notice.getCateId() !=null ){
            //按照【二级分类】查询
            return noticeMapper.selectNoticeList(notice);

        }else if(cate1Id!=null){
            // 如果带有cate1Id参数，就根据【一级分类】查询
            //先找出该一级分类下面的二级分类
            List<NoticeCategory> cate2in1List
                    = noticeCategoryMapper.selectNoticeCategoryListByParentId(cate1Id);
            //再按照【一级分类】查询
            return noticeMapper.selectNoticeListByCate1(notice,cate2in1List);

        }

        //在所有分类中查询(可以带title查询)
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增养老资讯
     * 
     * @param notice 养老资讯
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNotice(Notice notice)
    {
        notice.setCreateTime(DateUtils.getNowDate());
        //添加养老资讯
        int rows = noticeMapper.insertNotice(notice);
        //添加与资讯关联的详情
        insertNoticeDetail(notice);
        return rows;
    }

    /**
     * 修改养老资讯
     * 
     * @param notice 养老资讯
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNotice(Notice notice)
    {
        noticeMapper.deleteNoticeDetailByNoticeId(notice.getId());
        insertNoticeDetail(notice);
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 批量删除养老资讯
     * 
     * @param ids 需要删除的养老资讯主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNoticeByIds(Long[] ids)
    {
        noticeMapper.deleteNoticeDetailByNoticeIds(ids);
        return noticeMapper.deleteNoticeByIds(ids);
    }

    /**
     * 删除养老资讯信息
     * 
     * @param id 养老资讯主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNoticeById(Long id)
    {
        noticeMapper.deleteNoticeDetailByNoticeId(id);
        return noticeMapper.deleteNoticeById(id);
    }

    /**
     * 新增资讯详情信息
     * 
     * @param notice 养老资讯对象
     */
    public void insertNoticeDetail(Notice notice)
    {
        NoticeDetail noticeDetail = notice.getNoticeDetail();
        Long id = notice.getId();
        if (StringUtils.isNotNull(noticeDetail))
        {
            //添加 NoticeId
            noticeDetail.setNoticeId(id);
            noticeDetailMapper.insertNoticeDetail(noticeDetail);
        }
    }
}
